<?php
namespace app\admin\controller;

use app\common\model\Article as ArticleModel;
use app\common\model\ArticleCategory as CategoryModel;
/**
 * 文章控制器
 */
class Article extends Admin 
{
	/**
	 * 文章列表
	 * @return array
	 */
	public function index(){
		$title = $this->request->param('title/s');
		$status = $this->request->param('status/d');

		$where = [];
		$map = [];
		$query = [];
		if(!empty($title)){
			$where[] = ['title','like','%'.$title.'%'];
			$query['title'] = $title;
		}				

		if(!empty($status)){
			$map['status'] = $status;
			$query['status'] = $status;
		}

		$list = ArticleModel::with('category')->where($where)->where($map)->order('id desc')->paginate(15, false, ['query' => $query]);

		$this->assign('list', $list);

		return $this->fetch();
	}

	/**
	 * 新增文章
	 */
	public function add(){
		if($this->request->isPost()){
			$data = $this->request->post();
			//验证
			$result = $this->validate($data,[
				'cate|分类' => 'require',
				'title|标题' => 'require',	
				'content|内容' => 'require'
			]);
			// 验证失败 输出错误信息
			if(true !== $result) $this->error($result);

			if($query = ArticleModel::create($data)){
				$this->success('新增成功');
			}else{
				$this->error('新增失败');
			}
		}else{
			$category = CategoryModel::getList();

			$this->assign('title','新增文章');
			$this->assign('category', $category);
			$this->assign('info', null);

			return $this->fetch('edit');
		}
	}

	/**
	 * 编辑文章
	 * @param  integer $id 文章ID
	 * @return mixed
	 */
	public function edit($id = 0){
		if($this->request->isPost()){
			$data = $this->request->post();
			//验证
			$result = $this->validate($data,[
				'cate|分类' => 'require',
				'title|标题' => 'require',	
				'content|内容' => 'require'
			]);
			// 验证失败 输出错误信息
			if(true !== $result) $this->error($result);

			if($query = ArticleModel::update($data)){
				$this->success('编辑成功');
			}else{
				$this->error('编辑失败');
			}
		}else{
			empty($id) && $this->error('参数错误');

			$category = CategoryModel::getList();
			$info = ArticleModel::where('id',$id)->find();

			$this->assign('title','编辑文章');
			$this->assign('info', $info);
			$this->assign('category', $category);
			return $this->fetch('edit');
		}
	}

	/**
	 * 删除文章
	 * @param  integer $id 文章ID
	 * @return mixed
	 */
	public function del($id = 0){
		empty($id) && $this->error('参数错误');

		if(is_array($id)){
			$map[] = ['id','in',$id];
		}elseif(is_numeric($id)){
			$map[] = ['id','=',$id];
		}

		if($query = ArticleModel::where($map)->delete()){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
	}

	/**
	 * 更新状态
	 * @return mixed
	 */
	public function update(){
		$data = $this->request->param();

		if(empty($data)){
			$this->error('参数错误');
		}	
		
		if($query = ArticleModel::update($data)){
			$this->success('更新成功');
		}else{
			$this->error('更新失败');
		}
	}

	/**
	 * 分类列表
	 * @return array
	 */
	public function category(){
		$list = CategoryModel::getList();

		$this->assign('list',$list);
		return $this->fetch();
	}

	/**
	 * 新增分类
	 * @param integer $pid 分类ID
	 */
	public function addCategory($pid = 0){
		if($this->request->isPost()){
			$data = $this->request->post();
			//验证
			$result = $this->validate($data,[
				'pid|父分类' => 'require',
				'name|分类名称' => 'require'
			]);
			// 验证失败 输出错误信息
			if(true !== $result) $this->error($result);

			if($query = CategoryModel::create($data)){
				$this->success('新增成功');
			}else{
				$this->error('新增失败');
			}
		}else{
			$plist = CategoryModel::getList();

			$this->assign('title','新增分类');
			$this->assign('plist', $plist); 
			$this->assign('info',null);
			$this->assign('pid', $pid);

			return $this->fetch('editCategory');
		}
	}

	/**
	 * 编辑分类
	 * @param  integer $id 分类ID
	 * @return mixed
	 */
	public function editCategory($id = 0){
		if($this->request->isPost()){
			$data = $this->request->post();
			//验证
			$result = $this->validate($data,[
				'pid|父分类' => 'require',
				'name|分类名称' => 'require'
			]);
			// 验证失败 输出错误信息
			if(true !== $result) $this->error($result);

			if($query = CategoryModel::update($data)){
				$this->success('编辑成功');
			}else{
				$this->error('编辑失败');
			}
		}else{
			empty($id) && $this->success('参数错误');

			$info = CategoryModel::where('id',$id)->find();
			$plist = CategoryModel::getList();

			$this->assign('title','编辑分类');
			$this->assign('plist', $plist); 
			$this->assign('info',$info);
			$this->assign('pid', $info['pid']);
			return $this->fetch('editCategory');
		}
	}

	/**
	 * 删除分类
	 * @param  integer $id 分类ID
	 * @return mixed
	 */
	public function delCategory($id = 0){
		empty($id) && $this->error('参数错误');

		if(is_array($id)){
			$map[] = ['id','in',$id];
		}elseif(is_numeric($id)){
			$map[] = ['id','=',$id];
		}

		if($query = CategoryModel::where($map)->delete()){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
	}

	/**
	 * 更新状态
	 * @return mixed
	 */
	public function updateCategory(){
		$data = $this->request->param();

		if(empty($data)){
			$this->error('参数错误');
		}

		if($query = CategoryModel::update($data)){
			$this->success('更新成功');
		}else{
			$this->error('更新失败');
		}
	}
}